package com.tau.iplayer.client.dialogs;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tau.iplayer.client.IPlayer;
import com.tau.iplayer.client.IPlayerUtils;
import com.tau.iplayer.client.models.Playlist;

public class AddPlaylistDialog extends DialogBox {

	private static final boolean IS_AUTO_HIDE = false;
	private static final boolean IS_MODAL = true;
	private static final boolean IS_ANIMATED = true;
	private static final boolean IS_GLASS = true;
	private static final String HEADER = "Add Playlist";
	private IPlayer iPlayer;
	private TextBox playlistNameBox;

	public AddPlaylistDialog(IPlayer iplayer) {
		this();
		this.iPlayer = iplayer;
	}

	public AddPlaylistDialog() {
		setAutoHideEnabled(IS_AUTO_HIDE);
		setModal(IS_MODAL);
		setAnimationEnabled(IS_ANIMATED);
		setGlassEnabled(IS_GLASS);
		setText(HEADER);
		createContents();
	}

	private void createContents() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		final Button addPlaylistButton = new Button("OK");
		addPlaylistButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				handleAddPlaylistClicked();
			}
		});
		addPlaylistButton.setStyleName("button");
		addPlaylistButton.setEnabled(false);
		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("button");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleCancelClick();
			}
		});
		horizontalPanel.add(addPlaylistButton);
		horizontalPanel.add(cancelButton);
		horizontalPanel.setSpacing(3);
		
		HorizontalPanel playlistNamePanel = new HorizontalPanel();
		Label playlistNameLabel = new Label("Playlist Name:");
		playlistNameLabel.setStyleName("standardLabel");
		playlistNameBox = new TextBox();
		playlistNameBox.setStyleName("addSongDialogTextBox");
		playlistNameBox.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				boolean isEnabled = !IPlayerUtils.isEmpty(playlistNameBox.getValue());
				addPlaylistButton.setEnabled(isEnabled);
			}
		});
		playlistNamePanel.add(playlistNameLabel);
		playlistNamePanel.add(playlistNameBox);


		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.add(playlistNamePanel);
		verticalPanel.add(horizontalPanel);

		verticalPanel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_CENTER);

		setWidget(verticalPanel);
	}

	private void handleCancelClick() {
		this.hide();
	}

	private void handleAddPlaylistClicked() {
		iPlayer.addPlaylist(new Playlist(playlistNameBox.getValue()));
		this.hide();
	}

}
